package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.entity.User_Exams_entity;
import com.example.computer_exam_training_sys.service.User_Exams_Service;
import com.example.computer_exam_training_sys.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/userexams")
@CrossOrigin
public class User_Exams_Controller {
    @Autowired
    private User_Exams_Service userExamsService;

    // 查询所有考试记录
    @GetMapping("/all")
    public List<User_Exams_entity> getAllExams() {
        return userExamsService.findallexams();
    }

    // 按userId条件查询
    @GetMapping("/byUser/{userId}")
    public List<User_Exams_entity> getExamsByUserId(@PathVariable Integer userId) {
        return userExamsService.findByUserId(userId);
    }
    
    // 添加考试记录
    @PostMapping("/add")
    public Result<Integer> addExam(@RequestBody User_Exams_entity exam) {
        // 如果没有设置创建时间，则使用当前时间
        if (exam.getCreateTime() == null) {
            exam.setCreateTime(LocalDateTime.now());
        }
        int result = userExamsService.addExam(exam);
        if (result > 0) {
            return Result.success(exam.getExamId(), "添加考试记录成功");
        } else {
            return Result.error("添加考试记录失败");
        }
    }
}
